package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallDistributionService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.vo.PromoteStatistics;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户提现
 */
@RestController
@RequestMapping("/wx/promote")
@Validated
public class WxUserPromoteController {
    private final Log logger = LogFactory.getLog(WxUserPromoteController.class);

    @Autowired
    private LitemallDistributionService distributionService;

    @Autowired
    private LitemallUserService userService;


    @GetMapping("statistics")
    public Object save(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        LitemallUser user = userService.findById(userId);
        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("avatar", user.getAvatar());
        data.put("userId", user.getId());
        data.put("nickname",user.getNickname());
        PromoteStatistics promote = distributionService.findPromoteStatistics(userId);

        int directCount = 0;
        int totalCount = 0;
        int indirectCount = 0;
        if(promote != null){
            if(promote.getDirectCount() != null){
                directCount = promote.getDirectCount();
            }
            if(promote.getTotalCount() != null){
                totalCount = promote.getTotalCount();
            }
            if(promote.getIndirectCount() != null){
                indirectCount = promote.getIndirectCount();
            }
        }

        data.put("directCount", directCount);
        data.put("totalCount", totalCount);
        data.put("indirectCount",indirectCount);

        return ResponseUtil.ok(data);
    }

    @GetMapping("list")
    public Object list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        return ResponseUtil.ok("");
    }
}
